package com.software.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.bean.CustomerInfoBean;
import com.software.bean.CustomerLoginBean;
import com.software.dto.UpdateAvatarImageDto;
import com.software.dto.UpdatePersonalDataDto;
import com.software.mapper.CustomerInfoMapper;
import com.software.mapper.CustomerLoginMapper;
import com.software.service.CustomerInfoService;
import com.software.utils.AssertUtil;
import com.software.utils.PhoneUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-03-19
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfoBean> implements CustomerInfoService {

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private CustomerLoginMapper loginMapper;

    @Override
    public IPage<CustomerInfoBean> getUserListForPage(Integer pageNum, Integer pageSize) {
        AssertUtil.isTrue(pageNum == null, "参数异常");
        AssertUtil.isTrue(pageSize == null, "参数异常");
        Page<CustomerInfoBean> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<CustomerInfoBean> iPage = customerInfoMapper.getUserListForPage(page);
        return iPage;
    }

    @Override
    public CustomerInfoBean getPersonalData(Integer id) {
        AssertUtil.isTrue(id == null, "参数异常");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(id);
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常");
        return customerInfoBean;
    }

    @Override
    public CustomerInfoBean updatePersonalData(UpdatePersonalDataDto updatePersonalDataDto) {
        // 参数校验
        AssertUtil.isTrue(updatePersonalDataDto.getId() == null, "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePersonalDataDto.getCustomerEmail()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePersonalDataDto.getMobilPhone()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updatePersonalDataDto.getCustomerName()), "参数异常");
        AssertUtil.isTrue(updatePersonalDataDto.getCustomerSex() == null || updatePersonalDataDto.getCustomerSex() == 0, "性别异常");
        AssertUtil.isTrue(PhoneUtils.isMobile(updatePersonalDataDto.getMobilPhone()) == false, "手机号格式错误");

        CustomerInfoBean update = new CustomerInfoBean();
        update.setId(updatePersonalDataDto.getId());
        update.setMobilePhone(updatePersonalDataDto.getMobilPhone());
        update.setCustomerEmail(updatePersonalDataDto.getCustomerEmail());
        update.setCustomerSex(updatePersonalDataDto.getCustomerSex());
        update.setCustomerName(updatePersonalDataDto.getCustomerName());
        int i = customerInfoMapper.updateById(update);
        AssertUtil.isTrue(i != 1, "更新失败");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(updatePersonalDataDto.getId());
        return customerInfoBean;
    }

    /**
     * @description:更新头像
     * @author: wyj
     * @date: 2024/3/30 0:10
     * @param:
     * @return:
     */
    @Transactional
    @Override
    public Integer updateAvatarImage(UpdateAvatarImageDto avatarImageDto) {
        AssertUtil.isTrue(avatarImageDto.getFile() == null, "参数异常");
        AssertUtil.isTrue(avatarImageDto.getCustomerId() == null, "参数异常");
        //判断用户是否存在
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(avatarImageDto.getCustomerId());
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常");
        String oldAvatarImage = customerInfoBean.getAvatarUrl();
        //判断是否是默认头像
        if (!oldAvatarImage.contains("default")) {
            //删除原本的头像
            Boolean result = deleteImage(customerInfoBean.getAvatarUrl());
            AssertUtil.isTrue(result == false, "文件操作异常");
        }
        String newAvatarImage = saveImage(avatarImageDto.getFile());
        customerInfoBean.setAvatarUrl(newAvatarImage);
        int i = customerInfoMapper.updateById(customerInfoBean);
        return i == 1 ? 1 : 0;
    }

    /**
     * @description:禁用账户
     * @author: wyj
     * @date: 2024/4/3 0:19
     * @param:
     * @return:
     */
    @Override
    public Integer disableUser(Integer id) {
        //参数校验
        AssertUtil.isTrue(id == null || id == 0, "参数异常");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(id);
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常");
        CustomerLoginBean customerLoginBean = loginMapper.selectById(customerInfoBean.getCustomerLoginId());
        AssertUtil.isTrue(customerLoginBean == null, "返回值异常");
        AssertUtil.isTrue(customerLoginBean.getStatus() == 0, "账户已被禁用");
        customerLoginBean.setStatus(0);
        int i = loginMapper.updateById(customerLoginBean);
        return i == 1 ? 1 : 0;
    }

    /**
     * @description:启用账户
     * @author: wyj
     * @date: 2024/4/3 0:39
     * @param:customerId
     * @return:
     */
    @Override
    public Integer enableUser(Integer id) {
        AssertUtil.isTrue(id == null || id == 0, "参数异常");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(id);
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常");
        CustomerLoginBean customerLoginBean = loginMapper.selectById(customerInfoBean.getCustomerLoginId());
        AssertUtil.isTrue(customerLoginBean == null, "返回值异常");
        customerLoginBean.setStatus(1);
        int i = loginMapper.updateById(customerLoginBean);
        return i == 1 ? 1 : 0;
    }

    private Boolean deleteImage(String avatarUrl) {
        String avatarURL = avatarUrl;
        String endURL = avatarURL.substring(avatarURL.lastIndexOf("/") + 1);
        String realPath = "E:/img";
        String filePath = realPath + "/" + endURL;
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        } else {
            return false;
        }
        return true;
    }

    private String saveImage(MultipartFile file) {
        String realPath = "E:/img";
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String imgURL = "http://localhost:8082/file/" + fileName +
                file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filePath = realPath + "/" + fileName + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgURL;

    }


}
